#include "srsettingsdialog.h"
#include "ui_srsettingsdialog.h"

#include <QFontDialog>
#include <QSettings>

SRSettingsDialog::SRSettingsDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SRSettingsDialog)
{
    ui->setupUi(this);
    init();
}

SRSettingsDialog::~SRSettingsDialog()
{
    delete ui;
}

void SRSettingsDialog::init()
{
    QSettings s;

    QFont font = this->font();
    if (font.fromString(s.value(INSTITUTION_NAME_FONT).toString()))
        ui->instNameEdit->setFont(font);
    ui->instNameEdit->setText(s.value(INSTITUTION_NAME_TEXT).toString());
    if (font.fromString(s.value(REPORT_TITLE_FONT).toString()))
        ui->reportTitleEdit->setFont(font);
    ui->reportTitleEdit->setText(s.value(REPORT_TITLE_TEXT).toString());
    if (font.fromString(s.value(PATIENT_INFO_FONT).toString()))
        ui->patientInfoEdit->setFont(font);
    if (font.fromString(s.value(REPORT_BODY_FONT).toString()))
        ui->reportBodyEdit->setFont(font);
    if (font.fromString(s.value(REPORT_TAIL_FONT).toString()))
        ui->reportTailEdit->setFont(font);
    if (font.fromString(s.value(FOOTNOTE_FONT).toString()))
        ui->footNoteEdit->setFont(font);
    ui->footNoteEdit->setText(s.value(FOOTNOTE_TEXT).toString());

    ui->imageWidthSpin->setValue(s.value(IMAGE_WIDTH, 150.0).toDouble());
    ui->imageHeightSpin->setValue(s.value(IMAGE_HEIGHT, 150.0).toDouble());

    ui->leftMarginSpin->setValue(s.value(PAGE_MARGIN_LEFT, 10.0).toDouble());
    ui->topMarginSpin->setValue(s.value(PAGE_MARGIN_TOP, 15.0).toDouble());
    ui->rightMarginSpin->setValue(s.value(PAGE_MARGIN_RIGHT, 10.0).toDouble());
    ui->bottomMarginSpin->setValue(s.value(PAGE_MARGIN_BOTTOM, 15.0).toDouble());

}

void SRSettingsDialog::on_instNameFontButton_clicked()
{
    QSettings settings;
    bool ok;
    QFont font;
    font.fromString(settings.value(INSTITUTION_NAME_FONT).toString());
    font = QFontDialog::getFont(&ok, font, this);
    if (ok) ui->instNameEdit->setFont(font);
}

void SRSettingsDialog::on_reportTitleFontButton_clicked()
{
    QSettings settings;
    bool ok;
    QFont font;
    font.fromString(settings.value(REPORT_TITLE_FONT).toString());
    font = QFontDialog::getFont(&ok, font, this);
    if (ok) ui->reportTitleEdit->setFont(font);
}

void SRSettingsDialog::on_patientInfoFontButton_clicked()
{
    QSettings settings;
    bool ok;
    QFont font;
    font.fromString(settings.value(PATIENT_INFO_FONT).toString());
    font = QFontDialog::getFont(&ok, font, this);
    if (ok) ui->patientInfoEdit->setFont(font);
}

void SRSettingsDialog::on_reportBodyFontButton_clicked()
{
    QSettings settings;
    bool ok;
    QFont font;
    font.fromString(settings.value(REPORT_BODY_FONT).toString());
    font = QFontDialog::getFont(&ok, font, this);
    if (ok) ui->reportBodyEdit->setFont(font);
}

void SRSettingsDialog::on_reportTailFontButton_clicked()
{
    QSettings settings;
    bool ok;
    QFont font;
    font.fromString(settings.value(REPORT_TAIL_FONT).toString());
    font = QFontDialog::getFont(&ok, font, this);
    if (ok) ui->reportTailEdit->setFont(font);
}

void SRSettingsDialog::on_footNoteFontButton_clicked()
{
    QSettings settings;
    bool ok;
    QFont font;
    font.fromString(settings.value(FOOTNOTE_FONT).toString());
    font = QFontDialog::getFont(&ok, font, this);
    if (ok) ui->footNoteEdit->setFont(font);
}

void SRSettingsDialog::on_okButton_clicked()
{
    QSettings settings;
    settings.setValue(INSTITUTION_NAME_FONT, ui->instNameEdit->font().toString());
    settings.setValue(INSTITUTION_NAME_TEXT, ui->instNameEdit->text());
    settings.setValue(REPORT_TITLE_FONT, ui->reportTitleEdit->font().toString());
    settings.setValue(REPORT_TITLE_TEXT, ui->reportTitleEdit->text());
    settings.setValue(PATIENT_INFO_FONT, ui->patientInfoEdit->font().toString());
    settings.setValue(REPORT_BODY_FONT, ui->reportBodyEdit->font().toString());
    settings.setValue(REPORT_TAIL_FONT, ui->reportTailEdit->font().toString());
    settings.setValue(FOOTNOTE_FONT, ui->footNoteEdit->font().toString());
    settings.setValue(FOOTNOTE_TEXT, ui->footNoteEdit->text());
    settings.setValue(IMAGE_WIDTH, ui->imageWidthSpin->value());
    settings.setValue(IMAGE_HEIGHT, ui->imageHeightSpin->value());
    settings.setValue(PAGE_MARGIN_LEFT, ui->leftMarginSpin->value());
    settings.setValue(PAGE_MARGIN_TOP, ui->topMarginSpin->value());
    settings.setValue(PAGE_MARGIN_RIGHT, ui->rightMarginSpin->value());
    settings.setValue(PAGE_MARGIN_BOTTOM, ui->bottomMarginSpin->value());

    accept();
}
